<?php
$dbh = new PDO('mysql:host=localhost;dbname=BlogDB' ,
                'root' , '' );
// Check if the email format is valid
function textValid($blanks) {
    $pattern = '' ;
    return preg_match($pattern, $blanks);
}
// Check if a title exists
function postExists($dbh, $id) {
    $sql = "SELECT COUNT(*) AS cnt FROM Entries
            WHERE id=".$dbh->quote($id);
    $row = $dbh->query($sql)->fetch();
    return !empty($row['cnt' ]);
}

// Insert a new post into the list
function insertPost($dbh, $Title, $Body) {
    $sql = "INSERT INTO Entries (Title, Body)
            VALUES (".$dbh->quote($Title).", ".dbh->quote($Body).")";
    return $dbh->exec($sql);
}

//Edit a post from the list
function editPost($dbh, $id, $Body) {
	$sql = "UPDATE Entries SET Body = (".dbh->quote($Body).") WHERE id =" .$dbh->quote($id);
	return $dbh->exec($sql);
	} 

//Delete a post from the list
function deletePost($dbh, $id) {
	$sql = "DELETE Entries WHERE id = " .$dbh->quote($id);
	return $dbh->exec($sql);
	}
	
// Get list of posts
function getPosts($dbh) {
    $sql = "SELECT id, Title FROM Entries";
    $subs = $dbh->query($sql)->fetchAll();
    //echo "hello from getSubscribers";
    //print_r($subs);
    return $subs;
}

$blogs = getPosts($dbh);
$error = '' ;
$success = '' ;
$blanks = isset($_POST['blanks' ]) ? $_POST['blanks' ] : '' ;
if ($_SERVER['REQUEST_METHOD' ] == 'POST' ) {
    if (!textValid($blanks)) {
        $error = "Field cannot be blank";
    } elseif ( postExists($dhb, $id)) {
	    $error = "Post does not exist";
		}
	elseif (insertPost($dbh, $Title, $Body)) {
        $success = "Thank you, Your blog has been posted.";
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head><title>Blog entry page</title></head>
  <body>
    <h2>Current Posts</h2>
    <div>
        <?php 
            //print_r($blogs);
            //echo '<br />';
            foreach($blog as $Title) {
                echo htmlentities($Title['title'], ENT_QUOTES); 
                echo '<br />';
            }
        ?>
    </div>
    <hr>

    <h2>Create a new Blog</h2>
    <div style="color: red">
       <?php echo htmlentities($error, ENT_QUOTES) ?>
    </div>
    <div style="color: green">
       <?php echo htmlentities($success, ENT_QUOTES) ?>
    </div>

    <form method="post" action="/Blog.php">
      <input type="text" name="Title" value ="Title" size="20" />
	  <input type="text" name="Body" value ="Body" size="720" />
      <input type="submit" value="Create New" />
	  <hr>
	  
	  <h2>Edit a Post</h2>
	  <input type="text" name="EditID" value="id" size="5"/>
	  <input type="text" name="EditBody" Value="Enter new text" size="720" />
	  <input type ="submit" value="Edit" />
	  <hr>
	  
	  <h2>Delete a Post</h2>
	  <input type="text" name="DeleteID" value="id" size="5" />
	  <input type ="submit" value="Delete" />
	  
    </form>
	
	<hr>
	
  </body>
</html>



